<?php

/**
 * This is the model class for table "{{object}}".
 *
 * The followings are the available columns in table '{{object}}':
 * @property integer $id
 * @property string $name
 * @property integer $country_id
 * @property integer $user_id
 * @property string $description
 * @property integer $price
 * @property integer $price_week
 * @property integer $price_month
 * @property double $price_clean
 * @property double $price_security_deposit
 * @property double $price_extra_people
 * @property string $house_rules
 * @property integer $city_id
 * @property integer $size
 * @property string $check_in
 * @property string $check_out
 * @property integer $accommodates
 * @property integer $bedrooms
 * @property integer $minimum_stay
 * @property string $crdate
 */
class Object extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Object the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{object}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		
		return array(
			array('name, country_id, city_id, street, house, description', 'required'),
			array('country_id, user_id, room_type_id, bed_type_id, currency_id, price, price_week, price_month, city_id, size, accommodates, bedrooms, minimum_stay, extra_people_guest,
				rating_accuracy, rating_communication, rating_cleanliness, rating_location, rating_check_in, rating_review, rating_average', 'numerical', 'integerOnly'=>true),
			array('price_clean, price_security_deposit, price_extra_people', 'numerical'),
			array('name, check_in, check_out, street, house, apartment', 'length', 'max'=>255),
			array('description, house_rules', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, country_id, city_id, street, house, apartment, user_id, description, room_type_id, bed_type_id, currency_id, price, price_week, price_month, price_clean, price_security_deposit, price_extra_people, house_rules, size, check_in, check_out, accommodates, bedrooms, minimum_stay, extra_people_guest, crdate', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			// user
			'user'			=>	array(self::BELONGS_TO, 'Users', 'user_id'),			
			// images object
			'mainImage'		=>	array(self::BELONGS_TO, 'Images', 'image_id'),
			'images'		=>	array(self::HAS_MANY, 'Images', 'object_id',
				'order'	=>	'id DESC',
			),
			// Description
			'currency'		=>	array(self::BELONGS_TO, 'Currency', 'currency_id'),
			'roomType'		=>	array(self::BELONGS_TO, 'RoomType', 'room_type_id'),
			'city'			=>	array(self::BELONGS_TO, 'City', 'city_id'),
			'country'		=>	array(self::BELONGS_TO, 'Country', 'country_id'),
			'amenities'		=>	array(self::HAS_MANY, 'ObjectAmenities', 'object_id',
				'order'=>	'amenities_id'
			),
			// Rating Review
			'marksCount' => array(self::STAT, 'Review', 'object_id'),
			// book it
			'book_it'		=>	array(self::HAS_MANY, 'Booked', 'object_id'),
			'booked_days'	=>	array(self::HAS_MANY, 'BookedTemp', 'object_id'),
			// comments
			'comments'		=>	array(self::HAS_MANY, 'Review', 'object_id',
				'condition'	=>	'text IS NOT NULL',
				'order'		=>	'crdate DESC'
			),
			// bookmark
			'checkBookmark' =>	array(self::HAS_ONE, 'Favorites', 'object_id',
				'select'	=>	'id',
				'condition'	=>	'user_id=:user',
				'params'	=>	array(':user'=>Yii::app()->user->id),
			),
			'countBookmark' =>	array(self::STAT, 'Favorites', 'object_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' 			=>	Yii::t("main", "Id"),
			'name' 			=>	Yii::t("main", "Name"),
			'country_id'	=>	Yii::t("main", "Country"),
			'city_id' 		=>	Yii::t("main", "City"),
			'street' 		=>	Yii::t("main", "Street"),
			'house' 		=>	Yii::t("main", "House"),
			'apartment' 	=>	Yii::t("main", "Apartment"),
			'user_id'		=>	Yii::t("main", "User"),
			'description' 	=>	Yii::t("main", "Description"),
			'room_type_id' 	=>	Yii::t("main", "Room Type"),
			'bed_type_id' 	=>	Yii::t("main", "Bed Type"),
			'currency_id' 	=>	Yii::t("main", "Currency"),
			'price' 		=>	Yii::t("main", "Price"),
			'price_week' 	=>	Yii::t("main", "Price Week"),
			'price_month' 	=>	Yii::t("main", "Price Month"),
			'price_clean' 	=>	Yii::t("main", "Price Clean"),
			'price_security_deposit'=>	Yii::t("main", "Price Security Deposit"),
			'price_extra_people'	=>	Yii::t("main", "Price Extra People"),
			'extra_people_guest' 	=>	Yii::t("main", "Extra People Guest"),
			'house_rules' 	=>	Yii::t("main", "House Rules"),
			'size' 			=>	Yii::t("main", "Size"),
			'check_in' 		=>	Yii::t("main", "Check In"),
			'check_out' 	=>	Yii::t("main", "Check Out"),
			'accommodates' 	=>	Yii::t("main", "Accommodates"),
			'bedrooms' 		=>	Yii::t("main", "Bedrooms"), '',
			'minimum_stay' 	=>	Yii::t("main", "Minimum Stay"),
			'crdate' 		=>	Yii::t("main", "Crdate"),
			'rating_accuracy'		=>	Yii::t("main", "Accuracy"),
			'rating_communication'	=>	Yii::t("main", "Communication"),
			'rating_cleanliness'	=>	Yii::t("main", "Cleanliness"),
			'rating_location'		=>	Yii::t("main", "Location"),
			'rating_check_in'		=>	Yii::t("main", "Check In"),
			'rating_review'			=>	Yii::t("main", "Value"),
			'rating_average'		=>	Yii::t("main", "Average"),
		);
	}
	
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->image_id == NULL)
				$this->published = 0;
			return true;
		}
		else
			return false;
	}
	public function afterSave()
    {        
        if($this->isNewRecord)
        {
			$booked_day = new BookedTemp;
			$booked_day->object_id	=	$this->id;
			$booked_day->booked_id	=	0;
			$date = new DateTime();
			$date->modify('-1 day');
			$booked_day->booked_date=	$date->format('Y-m-d');
			$booked_day->save();
        }
    	else 
    		return false;
	}

	/**
	 * Rating Review
	 */
	public function CheckReview($id = NULL)
	{
		if(!isset($id)) $id = $this->id;
		$booked = Booked::model()->findByAttributes(array(
			'user_id'	=>	Yii::app()->user->id,
			'object_id'	=>	$id,
			'paid'		=>	1,
		));
		if (empty($booked))
			return false;
		else
			return true;
	}
	public function SetCheckReview($id = NULL)
	{
		if(!isset($id)) $id = $this->id;
		$booked = Booked::model()->findByAttributes(array(
			'user_id'	=>	Yii::app()->user->id,
			'object_id'	=>	$id,
			'paid'		=>	1,
		));
		if (empty($booked))
			return false;
		else
		{
			$booked->paid = 2;
			$booked->save();
		}
	}
	public function GetFormattedRating($attribute)
    {
		$rating = $this->$attribute;
		$rating = round($rating / 100 );
		// echo $rating;
		return $rating;
    }
	public function GetAverageRating()
	{
		$rating = $this->rating_average;
		$rating = round($rating / 10);
		// echo $rating;
		return $rating;
	}
	public function GetAverageRatingNum()
	{
		$rating = $this->rating_average;
		$rating = number_format( ($rating / 2 / 100), 1);
		return $rating;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('country_id',$this->country_id);
		$criteria->compare('city_id',$this->city_id);
		$criteria->compare('street',$this->street,true);
		$criteria->compare('house',$this->house,true);
		$criteria->compare('apartment',$this->apartment,true);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('room_type_id',$this->room_type_id);
		$criteria->compare('bed_type_id',$this->bed_type_id);
		$criteria->compare('currency_id',$this->currency_id);
		$criteria->compare('price',$this->price);
		$criteria->compare('price_week',$this->price_week);
		$criteria->compare('price_month',$this->price_month);
		$criteria->compare('price_clean',$this->price_clean);
		$criteria->compare('price_security_deposit',$this->price_security_deposit);
		$criteria->compare('price_extra_people',$this->price_extra_people);
		$criteria->compare('house_rules',$this->house_rules,true);
		
		$criteria->compare('size',$this->size);
		$criteria->compare('check_in',$this->check_in,true);
		$criteria->compare('check_out',$this->check_out,true);
		$criteria->compare('accommodates',$this->accommodates);
		$criteria->compare('bedrooms',$this->bedrooms);
		$criteria->compare('minimum_stay',$this->minimum_stay);
		$criteria->compare('crdate',$this->crdate,true);
		$criteria->compare('rating_accuracy',$this->rating_accuracy);
		$criteria->compare('rating_communication',$this->rating_communication);
		$criteria->compare('rating_cleanliness',$this->rating_cleanliness);
		$criteria->compare('rating_location',$this->rating_location);
		$criteria->compare('rating_check_in',$this->rating_check_in);
		$criteria->compare('rating_review',$this->rating_review);
		$criteria->compare('rating_average',$this->rating_average);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}